package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.log.MyLog;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查询购物车列表
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        Long currentId = (Long) BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //判断userID与当前登录用户的id相同
        lqw.eq(ShoppingCart::getUserId,currentId);
        //按添加时间的倒序排列
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        //查询
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
//    @MyLog("添加购物车")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("接收到添加购物车的请求:{}", shoppingCart);
        //获取当前登录的用户ID
        Long currentId = (Long) BaseContext.getCurrentId();
        //1.查询当前购物车中有没有该菜品或套餐
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //判断userID与当前登录用户id是否相同
        lqw.eq(ShoppingCart::getUserId,currentId);
        //查询是否有菜品
        lqw.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId());
        //查询是否有套餐
        lqw.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(lqw);
        //如果有就+1
        if (shoppingCartOne != null) {
            shoppingCartOne.setNumber(shoppingCartOne.getNumber() + 1);
            shoppingCartService.updateById(shoppingCartOne);
            return R.success(shoppingCartOne);
        }else{//如果没有,添加
            shoppingCart.setUserId(currentId);
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);

        }
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")

    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //判断当前userID与登录用户的id相同
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        //删除购物车数据
        shoppingCartService.remove(lqw);
        return R.success("清空成功");
    }

    /**
     * -1
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //获取当前登录的用户ID
        Long currentId = BaseContext.getCurrentId();
        //1.查询当前购物车中有没有该菜品或套餐
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //判断userID与当前登录用户id是否相同
        lqw.eq(ShoppingCart::getUserId,currentId);
        //查询是否有菜品
        lqw.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId());
        //查询是否有套餐
        lqw.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart cart = shoppingCartService.getOne(lqw);
        //如果>1的话,就-1
        if (cart.getNumber() == 1) {
            shoppingCartService.removeById(cart.getId());
            return R.success(cart);
        }
        cart.setNumber(cart.getNumber() - 1);
        shoppingCartService.updateById(cart);
        return R.success(cart);

    }
}
